package com.yuanheng100.ems4j.mapper;

import com.yuanheng100.ems4j.model.Ems4jEmail;
import com.yuanheng100.ems4j.model.Ems4jPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class Ems4jEmailSqlProvider
{

    public String selectCountByCondition(@Param("conditions") final Map<String, Object> conditions)
    {
        return new SQL()
        {
            {
                SELECT("COUNT(*)");
                FROM("ems4j_email");
                if (conditions.get("emailId") != null)
                {
                    WHERE("emailId = #{emailId,jdbcType=INTEGER}");
                }
                if (conditions.get("sender") != null)
                {
                    WHERE("sender = #{sender,jdbcType=VARCHAR}");
                }
                if (conditions.get("content") != null)
                {
                    WHERE("content = #{content,jdbcType=VARCHAR}");
                }
                if (conditions.get("receiver") != null)
                {
                    WHERE("receiver = #{receiver,jdbcType=VARCHAR}");
                }
                if (conditions.get("fromName") != null)
                {
                    WHERE("fromName = #{fromName,jdbcType=VARCHAR}");
                }
                if (conditions.get("toName") != null)
                {
                    WHERE("toName = #{toName,jdbcType=VARCHAR}");
                }

                if (conditions.get("emailSubject") != null)
                {
                    WHERE("emailSubject = #{emailSubject,jdbcType=VARCHAR}");
                }
                if (conditions.get("result") != null)
                {
                    WHERE("result = #{result,jdbcType=SMALLINT}");
                }
            }
        }.toString();
    }

    public String selectListByCondition(@Param("conditions") final Map<String, Object> conditions, @Param("page") final Ems4jPage<Ems4jEmail> page)
    {
        return new SQL()
        {
            {
                SELECT("*");
                FROM("ems4j_email");
                if (conditions.get("emailId") != null)
                {
                    WHERE("emailId = #{conditions.emailId,jdbcType=INTEGER}");
                }
                if (conditions.get("sender") != null)
                {
                    WHERE("sender = #{conditions.sender,jdbcType=VARCHAR}");
                }
                if (conditions.get("content") != null)
                {
                    WHERE("content = #{conditions.content,jdbcType=VARCHAR}");
                }
                if (conditions.get("receiver") != null)
                {
                    WHERE("receiver = #{conditions.receiver,jdbcType=VARCHAR}");
                }
                if (conditions.get("fromName") != null)
                {
                    WHERE("fromName = #{conditions.fromName,jdbcType=VARCHAR}");
                }
                if (conditions.get("toName") != null)
                {
                    WHERE("toName = #{conditions.toName,jdbcType=VARCHAR}");
                }

                if (conditions.get("emailSubject") != null)
                {
                    WHERE("emailSubject = #{conditions.emailSubject,jdbcType=VARCHAR}");
                }
                if (conditions.get("result") != null)
                {
                    WHERE("result = #{conditions.result,jdbcType=SMALLINT}");
                }
                if (page.getOrderBy() != null)
                {
                    ORDER_BY(page.getOrderBy() + " " + (page.getOrder() == null ? "ASC" : page.getOrder()));
                }
            }
        }.toString() + " LIMIT " + (page.getPageNo() - 1) * page.getPageSize() + "," + page.getPageSize();
    }

    public String insertSelective(Ems4jEmail record)
    {
        BEGIN();
        INSERT_INTO("ems4j_email");

        if (record.getEmailId() != null)
        {
            VALUES("emailId", "#{emailId,jdbcType=INTEGER}");
        }

        if (record.getSender() != null)
        {
            VALUES("sender", "#{sender,jdbcType=VARCHAR}");
        }

        if (record.getContent() != null)
        {
            VALUES("content", "#{content,jdbcType=VARCHAR}");
        }

        if (record.getReceiver() != null)
        {
            VALUES("receiver", "#{receiver,jdbcType=VARCHAR}");
        }

        if (record.getFromName() != null)
        {
            VALUES("fromName", "#{fromName,jdbcType=VARCHAR}");
        }

        if (record.getToName() != null)
        {
            VALUES("toName", "#{toName,jdbcType=VARCHAR}");
        }

        if (record.getEmailSubject() != null)
        {
            VALUES("emailSubject", "#{emailSubject,jdbcType=VARCHAR}");
        }

        if (record.getSendTime() != null)
        {
            VALUES("sendTime", "#{sendTime,jdbcType=TIMESTAMP}");
        }

        if (record.getResult() != null)
        {
            VALUES("result", "#{result,jdbcType=SMALLINT}");
        }

        return SQL();
    }

    public String updateByPrimaryKeySelective(Ems4jEmail record)
    {
        BEGIN();
        UPDATE("ems4j_email");

        if (record.getSender() != null)
        {
            SET("sender = #{sender,jdbcType=VARCHAR}");
        }

        if (record.getContent() != null)
        {
            SET("content = #{content,jdbcType=VARCHAR}");
        }

        if (record.getReceiver() != null)
        {
            SET("receiver = #{receiver,jdbcType=VARCHAR}");
        }

        if (record.getFromName() != null)
        {
            SET("fromName = #{fromName,jdbcType=VARCHAR}");
        }

        if (record.getToName() != null)
        {
            SET("toName = #{toName,jdbcType=VARCHAR}");
        }

        if (record.getEmailSubject() != null)
        {
            SET("emailSubject = #{emailSubject,jdbcType=VARCHAR}");
        }

        if (record.getSendTime() != null)
        {
            SET("sendTime = #{sendTime,jdbcType=TIMESTAMP}");
        }

        if (record.getResult() != null)
        {
            SET("result = #{result,jdbcType=SMALLINT}");
        }

        WHERE("emailId = #{emailId,jdbcType=INTEGER}");

        return SQL();
    }
}